package io.vector.study.springboot.api;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by vector on 2017/4/28.
 */
@RestControllerAdvice(basePackages = {"io.vector.study.springboot"})
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(ApiException.class)
    public Object handleApiException(ApiException apiException, HttpServletRequest request, WebRequest webRequest) {
//        return ApiResponse.error(apiException);
        return handleExceptionInternal(apiException,
                new ApiResponse.ErrorResponse(apiException.getCode(), apiException.getMessage()), null, HttpStatus.UNPROCESSABLE_ENTITY, webRequest);
    }

}
